package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderService.class)
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private MemberDao memberDao;

    @Override
    @Transactional
    public Result order(Map<String, String> map) throws Exception {
        //1. 判断是否设置过预约 , 如果当天没有设置过预约则无法预约
        Date orderDate = DateUtils.parseString2Date(map.get("orderDate"));
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(orderDate);
        if (orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        //2. 判断是否约满 , 如果当天约满则无法预约
        if (orderSetting.getNumber() <= orderSetting.getReservations()) {
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //3. 检查用户是否是会员, 如果不是会员, 自动注册为会员 ,
        //根据手机号查询会员信息
        String phone = map.get("telephone");
        Member member = memberDao.findByTelephone(phone);
        if (member == null) {
            //该用户未注册 还不是会员, 需要注册会员
            //构造会员对象
            member = new Member();
            member.setPhoneNumber(phone);
            member.setIdCard(map.get("idCard"));
            member.setName(map.get("name"));
            member.setSex(map.get("sex"));
            member.setRegTime(new Date());
            //保存会员信息
            memberDao.add(member);
        }

        //4. 判断是否重复预约  , 如果同一天已经预约过 , 则无法预约
        Order condition = new Order();
        condition.setMemberId(member.getId());
        condition.setSetmealId(Integer.parseInt(map.get("setmealId")));
        condition.setOrderDate(orderDate);
        //查询预约数据
        List<Order> orderList = orderDao.findByCondition(condition);
        //有预约数据, 重复预约
        if (orderList != null && orderList.size() > 0) {
            return new Result(false, MessageConstant.HAS_ORDERED);
        }

        //没有预约数据 , 可以进行预约
        //构造预约对象
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setSetmealId(Integer.parseInt(map.get("setmealId")));
        order.setOrderDate(orderDate);
        order.setOrderType(map.get("orderType"));
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        //保存预约数据
        orderDao.add(order);

        //5. 预约成功更新预约人数
        //可以预约，设置预约人数加一
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);

        return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());
    }

    @Override
    public Order findById(Integer id) {
        return orderDao.findById(id);
    }

    @Override

    public List<Integer> findOrderCountByDate(List dates) {
        List<Integer> list = new ArrayList();
        dates.forEach(date -> {
                    Integer count = orderDao.findOrderCountByDate((String) date);
                    list.add(count);
                }
        );
        return list;
    }

    @Override
    public List<Integer> findVisitsCountByDate(List<String> dates) {
        List<Integer> list = new ArrayList();
        dates.forEach(date -> {
                    Integer count = orderDao.findVisitsCountByDate(date);
                    list.add(count);
                }
        );
        return list;
    }
    

    public List<Order> selectHistory(Integer id, String startTime, String endTime) {
        return orderDao.selectHistory(id,startTime,endTime);
    }

}
